package com.andatsoft.laisim.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.andatsoft.laisim.R;
import com.andatsoft.laisim.theme.Theme;
import com.andatsoft.laisim.theme.ThemeManager;

public class VisualizerChooserDialogAdapter extends ArrayAdapter<String> {

	private String[] mItemList;
	private List<Integer> mItemsSelected;
	private LayoutInflater mInflater;
	private Theme mTheme;

	// private int mSelectedIndex;

	public VisualizerChooserDialogAdapter(Context context, String[] items,
			List<Integer> itemsSelected) {
		super(context, 0);
		this.mItemList = items;
		this.mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mTheme = ThemeManager.getSelectedTheme();
		mItemsSelected = itemsSelected;
	}

	public class ViewHolder {
		public TextView tvName;
	}

	@Override
	public int getCount() {
		if (mItemList == null)
			return 0;
		return mItemList.length;
	}

	@SuppressLint("NewApi")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		ViewHolder vHolder;
		if (view == null) {
			view = mInflater
					.inflate(R.layout.dialog_visualizer_chooser_list_item,
							parent, false);
			vHolder = new ViewHolder();
			vHolder.tvName = (TextView) view
					.findViewById(R.id.tv_visualizer_chooser_dialog_name);
			view.setTag(vHolder);
		} else {
			vHolder = (ViewHolder) view.getTag();
		}
		vHolder.tvName.setText(mItemList[position]);
		if (mItemsSelected.contains(position)) {
			vHolder.tvName.setTextColor(mTheme.getDialogTextSelectedColor());
		} else {
			vHolder.tvName.setTextColor(mTheme.getDialogTextColor());
		}

		return view;
	}

	public void setItemsSelected(List<Integer> items) {
		mItemsSelected = items;
		notifyDataSetChanged();
	}
}
